﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ePioneer_ep_pioneer_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            if (Request["id"]!=null&&Request["id"]!="0")
            {
                string strId = Request["id"].ToString();
                //修改
                this.hid_id.Value = strId;
                CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
                CMSModel.ePioneer_Type epr = null;
                int rid = Convert.ToInt32(this.hid_id.Value);
                epr = dal.ePioneer_Type.First(x => x.id == rid);
                this.txt_name.Text = epr.name;
                this.Image1.ImageUrl = epr.img;
                this.txt_brief.Text = epr.title ;
                this.txt_content.Text = epr.content;
               
            }
            else
            {
                this.hid_id.Value = "0";
            }

        }
    }
    protected void BtnModify_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        CMSModel.ePioneer_Type epr = null;

        string imgPath = System.Configuration.ConfigurationManager.AppSettings["imgPath"].ToString();      //imgpath
        string filePath = System.Configuration.ConfigurationManager.AppSettings["filePath"].ToString();       //filepath
        string strImgPath = "";
        if (FileUpload1.HasFile)
        {
            var data = FileUpload1.FileBytes;
            var fileName = FileUpload1.FileName;
            string msg = "";
            string path = filePath+"pioneer/" + fileName;
            strImgPath = imgPath + "pioneer/" + fileName;
            Deliver.AsynchronousClient.StartTranCommand(data, path, out msg);


        }


        if (this.hid_id.Value != "0")
        {
            int rid = Convert.ToInt32(this.hid_id.Value);
            epr = dal.ePioneer_Type.First(x => x.id == rid);
            if (strImgPath != "")
            {
                epr.img = strImgPath;
            }

            epr.name = this.txt_name.Text;

            epr.title = this.txt_brief.Text;
            epr.content = this.txt_content.Text;

            dal.SaveChanges();
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改成功');document.location='ep_pioneer.aspx'", true);
        }
        else
        {
            dal.ePioneer_Type.AddObject(new CMSModel.ePioneer_Type
            {
                name = this.txt_name.Text,
                img = strImgPath,
                title = this.txt_brief.Text,
                content = this.txt_content.Text
               
            });
            try
            {
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('新增成功');document.location='ep_pioneer.aspx'", true);
            }
            catch (Exception ex)
            { }


        }
    }
}